# -*- coding:utf-8 -*-

from flask import Flask, session, g, render_template
from flask_migrate import Migrate

import config
from blueprints.auth import bp as auth_bp
from blueprints.sm import bp as sm_bp
from decorators import login_required
from exts import db
from models import Usermodel

app = Flask(__name__)

# 绑定配置文件
app.config.from_object(config)

db.init_app(app)

migrate = Migrate(app, db)

app.register_blueprint(sm_bp)
app.register_blueprint(auth_bp)


# http://127.0.0.1:5000

@app.route('/')
@login_required
def index():
    return render_template("index.html")


@app.before_request
def my_before_request():
    userid = session.get("userid")
    if userid:
        user = Usermodel.query.get(userid)
        setattr(g, "user", user)
    else:
        setattr(g, "user", None)


@app.context_processor
def my_context_processor():
    return {"user": g.user}


# 接下去4句代码是用来测试与数据库是否连接成功用的
# with app.app_context():
# with db.engine.connect() as conn:
# result = conn.execute(text("select 1"))
# print(result.fetchone())  # (1,)

if __name__ == '__main__':
    app.run(host='0.0.0.0')
